package org.clipjoint.service.impl;

import org.clipjoint.dao.UserAdminDao;
import org.clipjoint.dao.impl.UserAdminDaoImpl;
import org.clipjoint.entity.dto.UserDto;
import org.clipjoint.entity.User;
import org.clipjoint.service.UserAdminService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 *
 * @date 2021/1/22
 */
public class UserAdminServiceImpl implements UserAdminService {
    private UserAdminDao userDao=new UserAdminDaoImpl();

    @Override
    public List<User> list(UserDto userDto) throws SQLException {
        return  userDao.selectAll(userDto);
    }

    @Override
    public Long count(UserDto build) throws SQLException {
        return userDao.selectCount(build);
    }

    @Override
    public int add(User user) throws SQLException {
        user.setCreate_time(new Date());
        user.setCredit(0l);
        user.setAccount(0l);
        return userDao.insert(user);
    }

    @Override
    public int deleteAll(List<Long> ids) {
        int result=0;
        for (Long id : ids) {
            try {
                result += userDao.deleteById(id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int del(Long id) throws SQLException {
        return userDao.deleteById(id);
    }

    @Override
    public User getOneById(Long id) throws SQLException {
        return userDao.selectOne(UserDto.builder().id(id).build());
    }

    @Override
    public int update(User user) throws SQLException {
        user.setLast_login_time(new Date());
        return userDao.update(user);
    }

    @Override
    public int addAll(List<User> users){
        int result=0;
        for (User user : users) {
            try {
                user.setCreate_time(new Date());
                result += userDao.insert(user);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }


}

